#!/bin/sh
#
# estbwadd
#	add an estbw printer description
#
#	Usage: estbwadd <queue name> <printer name or IP address>
#
# Copyright (c) 2017, Toshiba (Australia) Pty. Limited
# All rights reserved.
#
# Version 7.90		2017/10/19
#

if [ $# -ne 2 ]
then
	echo "Usage: $0 <queue name> <printer name/IP>"
	exit 1
fi

osType=`uname -s`
printer=`basename $1`

if [ $osType = "AIX" ]
then

	if [ -f /opt/toshiba/tap/filter/$1.conf ]
	then
		echo "Printer $1 already exists, please remove before re-installing."		
		echo "Exiting install."
		exit 1
	fi

	echo "Doing ESTBW installation for $printer on AIX"

	device="dev_"$printer
	printer_conf="/opt/toshiba/tap/filter/$1.conf"

	if [ ! -d /opt/toshiba/tap/filter ]
	then
		mkdir /opt/toshiba/tap/filter
	fi

	echo "# Copyright (c) 2017, Toshiba (Australia) Pty. Limited" > $printer_conf
	echo "# All rights reserved." >> $printer_conf
	echo "#" >> $printer_conf
	echo "# This file contains the configuration defaults for $printer" >> $printer_conf
	echo "# lines beginning with a '#' are comments" >> $printer_conf
	echo "#" >> $printer_conf
	echo "dest=$2		# IP or printer name" >> $printer_conf

	#
	# remove old 'formatted' man entries
	#

	if [ -f /usr/share/man/cat1/net_estbw.1 ]
	then
		/usr/bin/rm -f /usr/share/man/cat1/net_estbw.1
	fi

	mkque -q$printer -a 's_statfilter = /usr/lib/lpd/bsdshort' -a 'up = TRUE' -a "host = $2" -a 'rq = print'
	mkquedev -q$printer -d$device -a 'backend = /opt/toshiba/tap/bin/estbwBackend'

	cp /usr/lib/lpd/pio/predef/net_estbw /opt/toshiba/tap/filter/$1

elif [ $osType = "SCO_SV" ]
then

	if [ -f /usr/spool/lp/admins/lp/interfaces/$1.conf ]
	then
		echo "Printer $1 already exists, please remove before re-installing."		
		echo "Exiting install."
		exit 1
	fi


	echo "Doing ESTBW installation for $printer on SCO_SV"

	printer_conf=/usr/spool/lp/admins/lp/interfaces/$1.conf

	echo "# Copyright (c) 2017, Toshiba (Australia) Pty. Limited" > $printer_conf
	echo "# All rights reserved." >> $printer_conf
	echo "#" >> $printer_conf
	echo "# This file contains the configuration defaults for $printer" >> $printer_conf
	echo "# lines beginning with a '#' are comments" >> $printer_conf
	echo "#" >> $printer_conf
	echo "dest=$2		# IP or printer name" >> $printer_conf

	chmod +x $printer_conf
	chown lp:lp $printer_conf

	#
	# remove old 'formatted' man entries
	#

	if [ -f /usr/man/cat1/net_estbw.1 ]
	then
		/bin/rm -f /usr/man/cat1/net_estbw.1
	fi

	/usr/lib/lpadmin -p $1 -v /dev/null -mnet_estbw

	accept $1
	enable $1


elif [ $osType = "SunOS" ]
then
	osVersion=`uname -v`
	if [ "$((${osVersion}>=11.2))" = "1" ]
	then
		use_cups=1
		interfaces_dir=/etc/cups/interfaces
	else
		unset use_cups
		interfaces_dir=/etc/lp/interfaces
	fi
	
	printer_conf=$interfaces_dir/$1.conf

	if [ -f $printer_conf ]
	then
		echo "Printer $1 already exists, please remove before re-installing."		
		echo "Exiting install."
		exit 1
	fi

	echo "Doing ESTBW installation for $printer on SunOS"

	echo "# Copyright (c) 2017, Toshiba (Australia) Pty. Limited" > $printer_conf
	echo "# All rights reserved." >> $printer_conf
	echo "#" >> $printer_conf
	echo "# This file contains the configuration defaults for $printer" >> $printer_conf
	echo "# lines beginning with a '#' are comments" >> $printer_conf
	echo "#" >> $printer_conf
	echo "dest=$2		# IP or printer name" >> $printer_conf

	chmod +x $printer_conf
	chown lp:lp $printer_conf

	#
	# remove old 'formatted' man entries
	#

	if [ -f /usr/share/man/cat1/net_estbw.1 ]
	then
		/usr/bin/rm -f /usr/share/man/cat1/net_estbw.1
	fi
	if [ -f /usr/share/man/fmt1/net_estbw.1 ]
	then
		/usr/bin/rm -f /usr/share/man/fmt1/net_estbw.1
	fi

	if [ -n "$use_cups" ]
	then
		lpadmin -p $1 -v file:///dev/null -o raw -i /usr/lib/lp/model/net_estbw
	else
		lpadmin -p $1 -v /dev/null -I "any" -i /usr/lib/lp/model/net_estbw
	fi
	
	accept $1
	enable $1
elif [ $osType = "HP-UX" ]
then

	if [ -f /etc/lp/interface/$1.conf ]
	then
		echo "Printer $1 already exists, please remove before re-installing."		
		echo "Exiting install."
		exit 1
	fi

	echo "Doing ESTBW installation for $printer on HP-UX"

	printer_conf=/etc/lp/interface/$1.conf

	echo "# Copyright (c) 2017, Toshiba (Australia) Pty. Limited" > $printer_conf
	echo "# All rights reserved." >> $printer_conf
	echo "#" >> $printer_conf
	echo "# This file contains the configuration defaults for $printer" >> $printer_conf
	echo "# lines beginning with a '#' are comments" >> $printer_conf
	echo "#" >> $printer_conf
	echo "dest=$2           # IP or printer name" >> $printer_conf

	chmod +x $printer_conf
	chown lp:lp $printer_conf


	#
	# remove old 'formatted' man entries
	#

	if [ -f /usr/share/man/cat1.Z/net_estbw.1 ]
	then
		/usr/bin/rm -f /usr/share/man/cat1.Z/net_estbw.1
	fi
	if [ -f /usr/share/man/cat1/net_estbw.1 ]
	then
		/usr/bin/rm -f /usr/share/man/cat1/net_estbw.1
	fi
	#
	# check if the schedule is running and if it
	# is we have to stop it and then restart it.
	#
	ps -ef | grep lpsched | grep -iv grep > /dev/null 2>&1
	nosched=$?

	if [ $nosched = 0 ]
	then
		/usr/sbin/lpshut > /dev/null 2>&1
	fi


	/usr/sbin/lpadmin -p$1 -v/dev/null -mnet_estbw -orm$2 -orpprint -ob3

	/usr/sbin/accept $1
	enable $1


	#
	# If it was running then start it again
	#
	if [ $nosched = 0 ]
	then
		/usr/sbin/lpsched > /dev/null 2>&1
	fi
elif [ $osType = "Linux" ]
then

	if [ -f /opt/toshiba/tap/interface/$1.conf ]
	then
		echo "Printer $1 already exists, please remove before re-installing."		
		echo "Exiting install."
		exit 1
	fi

	echo "Doing ESTBW installation for $printer on Linux"

	printer_conf="/opt/toshiba/tap/interface/$1.conf"

	if [ ! -d /opt/toshiba/tap/interface ]
	then
		mkdir /opt/toshiba/tap/interface
	fi

	echo "# Copyright (c) 2017, Toshiba (Australia) Pty. Limited" > $printer_conf
	echo "# All rights reserved." >> $printer_conf
	echo "#" >> $printer_conf
	echo "# This file contains the configuration defaults for $printer" >> $printer_conf
	echo "# lines beginning with a '#' are comments" >> $printer_conf
	echo "#" >> $printer_conf
	echo "dest=$2		# IP or printer name" >> $printer_conf

	#
	# remove old 'formatted' man entries
	#
	if [ -f /var/catman/cat1/net_estbw.1.gz ]
	then
		/bin/rm -f /usr/catman/cat1/net_estbw.1.gz
	fi

#
#	Copy the filter to the interface directory and add the printer
#	to the printcap file.
#
	cp /opt/toshiba/tap/model/net_estbw /opt/toshiba/tap/interface/$printer
	/opt/toshiba/tap/bin/modPrintcap -a $printer $2
	if [ -f /opt/toshiba/tap/bin/flag_$printer ]
	then
		/opt/toshiba/tap/bin/printconf_import -a $printer $2
		rm /opt/toshiba/tap/bin/flag_$printer
	fi
	echo "Please restart lpd !"
else

	if [ -f /etc/lp/interfaces/$1.conf ]
	then
		echo "Printer $1 already exists, please remove before re-installing."		
		echo "Exiting install."
		exit 1
	fi

	echo "Doing ESTBW installation for $printer on Caldera OpenUNIX Default ?"

	printer_conf=/etc/lp/interfaces/$1.conf

	echo "# Copyright (c) 2017, Toshiba (Australia) Pty. Limited" > $printer_conf
	echo "# All rights reserved." >> $printer_conf
	echo "#" >> $printer_conf
	echo "# This file contains the configuration defaults for $printer" >> $printer_conf
	echo "# lines beginning with a '#' are comments" >> $printer_conf
	echo "#" >> $printer_conf
	echo "dest=$2		# IP or printer name" >> $printer_conf

	chmod +x $printer_conf
	chown lp:lp $printer_conf

	#
	# remove old 'formatted' man entries
	#

	if [ -f /usr/share/man/cat1/net_estbw.1 ]
	then
		/usr/bin/rm -f /usr/share/man/cat1/net_estbw.1
	fi
	if [ -f /usr/share/man/fmt1/net_estbw.1 ]
	then
		/usr/bin/rm -f /usr/share/man/fmt1/net_estbw.1
	fi

	lpadmin -p $1 -v /dev/null -I "any" -i /usr/lib/lp/model/net_estbw

	accept $1
	enable $1
fi

